package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.SmsRecordDTO;
import com.pshcp.domain.model.SmsRecord;
import com.pshcp.mapper.SmsRecordMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 短信发送记录表(与供应商记录) 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class SmsRecordService {

    @Autowired
    private SmsRecordMapper smsRecordMapper;

    public PageList<SmsRecord> list(SmsRecordDTO smsRecordDTO){
        if(smsRecordDTO.getPageNum() != null && smsRecordDTO.getPageSize() != null){
            PageHelper.startPage(smsRecordDTO.getPageNum(), smsRecordDTO.getPageSize());
        }
        Example example = new Example(SmsRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(smsRecordDTO.getStatus() != null){
            criteria.andEqualTo("status", smsRecordDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<SmsRecord> list = smsRecordMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<SmsRecord> select(SmsRecord smsRecord){
        return smsRecordMapper.select(smsRecord);
    }

    public int insert(SmsRecord smsRecord){
        smsRecord.setId(SystemUtil.getId());
        smsRecord.setStatus(CommonStatusEnum.INIT.getCode());
        smsRecord.setCreatedTime(new Date());
        return smsRecordMapper.insert(smsRecord);
    }

    public int update(SmsRecord smsRecord){
        smsRecord.setUpdateTime(new Date());
        return smsRecordMapper.updateByPrimaryKeySelective(smsRecord);
    }

    public SmsRecord findById(Long id){
        return smsRecordMapper.selectByPrimaryKey(id);
    }

    public List<SmsRecord> findByIds(List<Long> ids){
        Example example = new Example(SmsRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return smsRecordMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(SmsRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        SmsRecord smsRecord = new SmsRecord();
        smsRecord.setStatus(CommonStatusEnum.DEL.getCode());
        return smsRecordMapper.updateByExampleSelective(smsRecord, example);
    }

}
